use crate::email;
use crate::{ApiError, ApiResult, ToError};
use lettre::AsyncTransport;
use lettre::message::header::ContentType;
use lettre::transport::smtp::response::Response;

/// 发送简单的邮件
pub async fn simple_email<S: AsRef<str>, B: Into<String>>(
    reveive: S,
    subject: B,
    body: B,
) -> ApiResult<Response> {
    let sender = email::get();
    let message = email::message()?
        .to(format!("<{}>", reveive.as_ref())
            .parse()
            .to_err(|| ApiError::Biz("接收人错误".into()))?)
        .subject(subject.into())
        .header(ContentType::TEXT_PLAIN)
        .body(body.into())
        .to_err(|| ApiError::Biz("创建消息错误".into()))?;

    sender
        .send(message)
        .await
        .to_err(|| ApiError::Biz("发送消息错误".into()))
}
